home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 729 / dsound / source / playmono.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  4KB  |  177 lines

  1.  
  2. /*************************************************************************/
  3. /*                   PlayMono.c                 */
  4. /*           Contains code used to play mono samples.          */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17.  
  18. #include <proto/intuition.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21.  
  22. UBYTE rightAMap[]={4,2,1,8};
  23. UBYTE leftAMap[]={1,8,2,4};
  24. UBYTE eitherAMap[]={1,2,4,8};
  25. UBYTE bothAMap[]={8,3,5,10};
  26.  
  27. extern UBYTE volume;
  28. extern UWORD speed;
  29. extern ULONG bufSize;
  30.  
  31. extern BOOL readAll;
  32. extern BOOL loop;
  33. extern struct Window *window;
  34.  
  35. extern ULONG signalMask;
  36.  
  37. /*Play a sample out of one speaker (left, right, or either)*/
  38. void playMonoSample(BPTR file,channel audioChannel,struct Voice8Header *vhdr,
  39.             ULONG len)
  40. {
  41.    struct IOAudio *iob1,*iob2,*curIob,*altIob;
  42.    ULONG toRead;
  43.    ULONG sampleSize=len;
  44.    BOOL done=FALSE;
  45.    UBYTE *allocationMap;
  46.  
  47.    /*Load the entire sample into memory, if the user so specified*/
  48.    if(readAll)
  49.    {
  50.       storeLeft(file,len,bufSize);
  51.       file=0L;
  52.    }
  53.  
  54.    /*Decide which audio channel will be allocated*/
  55.    switch(audioChannel)
  56.    {
  57.       case MONO_LEFT:
  58.      allocationMap=leftAMap;
  59.      break;
  60.       case MONO_RIGHT:
  61.      allocationMap=rightAMap;
  62.      break;
  63.       case UNSPECIFIED:
  64.      allocationMap=eitherAMap;
  65.      break;
  66.    }
  67.  
  68.    /*Get the first audio channel*/
  69.    iob1=GetAudioChannel(bufSize,allocationMap);
  70.    if(iob1==NULL)
  71.    {
  72.       WriteMsg("Couldn't create the first buffer\n");
  73.       cleanup(150);
  74.    }
  75.  
  76.    /* If the user didn't specify a volume, get it from the VHDR */
  77.    if(volume==0)
  78.       volume=(vhdr->volume>>10);
  79.  
  80.    /* If the VHDR gave a volume of zero, use maximum volume*/
  81.    if(volume==0)
  82.       volume=64;
  83.  
  84.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  85.    /* rate found in the VHDR) */
  86.    if(speed==0)
  87.       speed=1000000000/(vhdr->samplesPerSec*279);
  88.    else
  89.       speed=1000000000/(speed*279);
  90.  
  91.    InitAudioChannel(iob1,volume,speed);
  92.  
  93.    /*Get the 2nd audio channel*/
  94.    iob2=DuplicateAudioChannel(iob1);
  95.  
  96.    if(iob2==NULL)
  97.    {
  98.       FreeAudioChannel(iob1);
  99.       WriteMsg("Couldn't create the second buffer\n");
  100.       cleanup(175);
  101.    }
  102.  
  103.    /* Load the first buffer*/
  104.    toRead=MIN(len,bufSize);
  105.    LoadAudioBuffer(file,iob1,toRead);
  106.  
  107.    len-=toRead;
  108.    if(len==0 && loop)
  109.    {
  110.       len=sampleSize;
  111.       Seek(file,-sampleSize,OFFSET_CURRENT);
  112.    }
  113.  
  114.    /*Store the number of samples to be played*/
  115.    iob1->ioa_Length=toRead;
  116.  
  117.    /*Queue up the play request*/
  118.    BeginIO((struct IORequest *)iob1);
  119.  
  120.    curIob=iob2;
  121.    altIob=iob1;
  122.  
  123.    /*Loop while there's stuff to read*/
  124.    while(!done)
  125.    {
  126.       toRead=MIN(len,bufSize);
  127.       if(toRead!=0)
  128.       {
  129.      LoadAudioBuffer(file,curIob,toRead);
  130.      curIob->ioa_Length=toRead;
  131.      BeginIO((struct IORequest *)curIob);
  132.      len-=toRead;
  133.      if(len==0 && loop)
  134.      {
  135.         len=sampleSize;
  136.         Seek(file,-sampleSize,OFFSET_CURRENT);
  137.      }
  138.      done=FALSE;
  139.       }
  140.       else
  141.      done=TRUE;
  142.  
  143.       /*Wait for the buffer to finish*/
  144.       if((Wait((1<<altIob->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit) |
  145.        signalMask) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  146.      done=TRUE;
  147.  
  148.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  149.       {
  150.      done=TRUE;
  151.       }
  152.  
  153.       swapPointers(&curIob,&altIob);
  154.    }
  155.  
  156.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  157.    /*how much memory to free*/
  158.    iob1->ioa_Length=iob2->ioa_Length=bufSize;
  159.  
  160.    FreeAudioChannel(iob1);
  161.    DeleteDuplication(iob2);
  162.  
  163.    return;
  164. }
  165.  
  166. void swapPointers(struct IOAudio **first,struct IOAudio **second)
  167. {
  168.    void *temp;
  169.  
  170.    temp=*first;
  171.    *first=*second;
  172.    *second=temp;
  173.  
  174.    return;
  175. }
  176. /*End of PlayMono.c*/
  177.